<?php
namespace application\datatypes;
use application\datatypes\DataType;

class MultiChoice extends DataType {
	public static $name = 'Multi-Choice';
	public static $fields = array(array('columnType'=>'VARCHAR(50)'));
	
	public static function validate($field, $data, $results) {
		$value = $data[$field->cf_column];
		if($field->cf_required && !$value) {
			$results['errors'][] = '<b>'.$field->cf_display_name.'</b> is required.';
		}
		elseif($value) {
			$results['db_fields'][$field->cf_column] = $value;	
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		?>
		
		<fieldset class="multichoice-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = $data[$field->cf_column];
		$options = DB()->getResults("SELECT * FROM `cn_content_field_values` WHERE `cfv_field`='$field->cf_id' ORDER BY `cfv_text2`");
		$optionsString = '';
		if($field->cf_aux == 'select') {
			foreach($options as $option) {
				$selected = ($option->cfv_text1== $value) ? ' selected="selected"' : '';
				$optionsString .= Modulator()->runFilter('load_options_select_string', '<option'.$selected.' value="'.$option->cfv_text1.'">'.$option->cfv_text2.'</option>', array('option'=>$option, 'value'=>$value, 'field'=>$field));
			}
			
			$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
					'element'=>
						sprintf('<select id="%1$s" name="%1$s">'.$optionsString.'</select>', $field->cf_column)));
		}
		else {
			foreach($options as $option) {
				$selected = ($option->cfv_text1 == $value) ? ' checked="checked"' : '';
				$optionsString .= Modulator()->runFilter('load_options_radio_string', '<label><input type="radio"'.$selected.' name="'.$field->cf_column.'" value="'.$option->cfv_text1.'"/>'.$option->cfv_text2.'</label>', array('option'=>$option, 'value'=>$value, 'field'=>$field));
			}
			
			$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
					'element'=>$optionsString));
		}
		return $formElements;
	
	}
	
	public static function save($field) {
		$values = $_REQUEST['multichoice_values'];
		$displays = $_REQUEST['multichoice_displays'];
		$deletes = $_REQUEST['multichoice_deletes'];
		$field = $field->getId();
		if(count($deletes)) {
			foreach($deletes as $delete) {
				DB()->query("DELETE FROM `cn_content_field_values` WHERE `cfv_field`='$field' AND `cfv_id`='$delete'");
			}	
		}
		if(count($values)) {
			foreach($values as $key=>$value) {
				$optionDisplay = $displays[$key];
				DB()->query("INSERT INTO `cn_content_field_values` (`cfv_field`, `cfv_text1`, `cfv_text2`) VALUES('$field', '$value', '$optionDisplay')");
			}
		}
		
	}
	
	
	public static function editForm($field) {
		?>
		<script type="text/javascript">
			$(document).ready(function() {
				$('#data-type-edit-add-option').click(function(e) {
					e.preventDefault();
					var option_class = $('#edit-options-body').children('tr:last').attr('class');
					if(option_class == 'alternate existing') {
						option_class = 'new';
					}
					else {
						option_class = 'alternate new';
					}
					$('#edit-options-body').append('<tr class="' + option_class + '"><td><input type="text" class="multiselect_value" name="multichoice_values[]"/></td><td><input type="text" name="multichoice_displays[]"/><td><input type="radio" class="multichoice_default" name="field_default"/></td><td><button class="data-type-multichoice-edit-delete small-button">X</button</td></tr>');
					
				});
				$('.data-type-multichoice-edit-delete').live('click', function() {
					$(this).parent('td').parent('tr').remove();
				});
			});
		</script>
		<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
		<ul class="form-fields">
			<li>
				<label>Field Display Name</label>
				<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</li>
			<li>
				<label>Field Description</label>
				<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</li>
			<li>
					<label><input type="checkbox" name="field_required" <?=($field->getRequired()) ? 'checked="checked"' : ''?> value="1"/> Required</label>
			</li>
			<li>
					<b>Display As</b>
					<?php $aux = DB()->getVar("SELECT `cf_aux` FROM `cn_content_fields` WHERE `cf_id`='$field->field_id'");?>
					<label><input type="radio" name="field_aux" <?=($aux == 'radio') ?  'checked="checked"' : ''?> value="radio"/> Radio Buttons</label>
					<label><input type="radio" name="field_aux" <?=($aux == 'select') ?  'checked="checked"' : ''?> value="select"/> Dropdown Box</label>
					
			</li>
			<li>
				<table class="admin-table" width="100%">
					<thead>
						<tr>
							<th width="40%">Value</th>
							<th width="40%">Display</th>
							<th width="10%">Default</th>
							<th width="10%">Delete</th>
							
						</tr>
					</thead>
					<tfoot>
						<tr>
							<th width="40%">Value</th>
							<th width="40%">Display</th>
							<th width="10%">Default</th>
							<th width="10%">Delete</th>
							
						</tr>
					</tfoot>
					<tbody id="edit-options-body">
						<?php $style = '';?>
						<?php $fieldID = $field->getId();?>
						<?php $options = DB()->getResults("SELECT * FROM `cn_content_field_values` WHERE `cfv_field`='$fieldID'");
						$default = DB()->getVar("SELECT `cf_default` FROM `cn_content_fields` WHERE `cf_id`='$fieldID'");
						foreach($options as $option) : ?>
						<tr<?=$style?>>
							<td class="value"><?=$option->cfv_text1?></td>
							<td><?=$option->cfv_text2?></td>
							<td><input type="radio" <?= ($option->cfv_id == $default) ? 'checked="checked"' : ''?> name="field_default" value="<?=$option->cfv_id?>"/></td>
							<td><input type="checkbox" name="multichoice_deletes[]" value="<?=$option->cfv_id?>"/></td>
							
						</tr>
						<?php $style = ($style == ' class="alternate existing"') ? ' class="existing"' : ' class="alternate existing"';?>
						<?php endforeach;?>
					</tbody>
				</table>
			</li>
			<li>
				<button id="data-type-edit-add-option" class="small-button">+ Add Option</button>
			</li
		</ul>
			<?php
	}
	
}
?>